package ass.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ass.beans.UserRecommendMovieBean;
import ass.dao.support.CinemaDAOImpl;
import ass.dao.support.UserRecommendMovieDAOImpl;

/**
 * Servlet implementation class recommendMovie
 */
public class recommendMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public recommendMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		System.out.println("movie id: "+id);
		
		HttpSession session = request.getSession(true);
		String username = (String) session.getAttribute("username");
		System.out.println(username);
		
		UserRecommendMovieBean userRecommendMovieBean = new UserRecommendMovieBean();
		userRecommendMovieBean.setUsername(username);
		userRecommendMovieBean.setMovie_id(id);
		
		UserRecommendMovieDAOImpl userRecommendMovieDAOImpl = new UserRecommendMovieDAOImpl();
		
		try {
			userRecommendMovieDAOImpl.insert(userRecommendMovieBean);
			request.setAttribute("Movie_Recommended",  "<font color='green'>Movie Recommended</font>");
			
		}catch (Exception e){
			e.printStackTrace();
			request.setAttribute("Movie_Recommended",  "<font color='red'>You have recommended this movie</font>");
			
		}
		
		response.setContentType("text/html");// from response, set content type
		PrintWriter out = response.getWriter();// from response, get output
												// writer
		out.write("Recommend Movie insert succesful");
		//out.close();
		
		String nextJSP = "";
		nextJSP = "/MovieDetail?id="+ id;
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
